#include "stdafx.h"
#include "LookFeelEditor.h"
#include "App.h"

LookFeelEditor * LookFeelEditor::msInstance = NULL;

LookFeelEditor::LookFeelEditor()
	: mEditLookFeel(NULL)
	, mEditSkin(NULL)
{
	msInstance = this;

	MGUI_SkinManager::Instance()->Load("UISkin.xml");

	mLookFeelPanel = new LookFeelPanel;
	mLookFeelProperty = new LookFeelProperty;
	mLookFeelSkinEd = new LookFeelSkinEd;

	App::Instance()->eventEditorModeChanged += OnEditorModeChanged(this, &LookFeelEditor::OnEditorModeChanged_);
}

LookFeelEditor::~LookFeelEditor()
{
	mEditLookFeel = NULL;
	mEditSkin = NULL;

	delete mLookFeelSkinEd;
	delete mLookFeelProperty;
	delete mLookFeelPanel;

	msInstance = NULL;
}

void LookFeelEditor::Layout()
{
	mLookFeelPanel->Layout();
	mLookFeelProperty->Layout();
	mLookFeelSkinEd->Layout();
}

void LookFeelEditor::SetEditLookFeel(MGUI_LookFeel * _lookfeel)
{
	if (mEditLookFeel != _lookfeel)
	{
		mEditLookFeel = _lookfeel;
		eventEditLookFeelChanged(_lookfeel);

		_notifySkinChanged(_lookfeel->GetSkin());
		_notifyClientChanged(_lookfeel->GetClientRect());
	}
}

int LookFeelEditor::GetEditWidgetType()
{
	return mLookFeelPanel->GetEditWidgetType();
}

MGUI_LookFeel * LookFeelEditor::GetEditLookFeel()
{
	return mEditLookFeel;
}

void LookFeelEditor::_notifyNameChanged()
{
	eventNameChanged();
}

void LookFeelEditor::_notifyRectChanged(const MGUI_Rect & _rect)
{
	mEditRect = _rect;

	eventEditRectChanged(_rect);
}

void LookFeelEditor::_notifyClientChanged(const MGUI_ClientRect & _rect)
{
	if (mEditLookFeel)
	{
		mEditLookFeel->SetClientRect(_rect);

		eventEditClientChanged(_rect);
	}
}

void LookFeelEditor::_notifySkinChanged(Texture * _skin)
{
	if (mEditLookFeel)
		mEditLookFeel->SetSkin(_skin);

	if (mEditSkin != _skin)
	{
		mEditSkin = _skin;
		eventEditSkinChanged(_skin);
	}

	if (mEditSkin)
	{
		TString128 text;

		text.Format("%s (%d x %d).", mEditSkin->GetSourceName().c_str(), mEditSkin->GetWidth(), mEditSkin->GetHeight());

		MainStatusBar::Instance()->SetText(text);
	}
}

void LookFeelEditor::Save()
{
	if (MGUI_LookFeelManager::Instance()->Save())
	{
		MessageBox(App::Instance()->_hWnd(), "Save OK!", "Info", MB_OK);
	}
}
	

void LookFeelEditor::OnEditorModeChanged_(eEditorMode _mode)
{
	if (_mode == EM_Lookfeel)
	{
		mLookFeelPanel->GetLayout()->SetVisible(true);
		mLookFeelProperty->GetLayout()->SetVisible(true);
		mLookFeelSkinEd->GetLayout()->SetVisible(true);

		if (mEditSkin)
		{
			TString128 text;

			text.Format("%s (%d x %d).", mEditSkin->GetSourceName().c_str(), mEditSkin->GetWidth(), mEditSkin->GetHeight());

			MainStatusBar::Instance()->SetText(text);
		}
	}
	else
	{
		mLookFeelPanel->GetLayout()->SetVisible(false);
		mLookFeelProperty->GetLayout()->SetVisible(false);
		mLookFeelSkinEd->GetLayout()->SetVisible(false);
	}
}
